const http = require('http')
const chalk = require('chalk')
const path =require('path')


const route = require('./src/helper/route')
const conf = require('./src/config/defaultConfig')
const openUrl = require('./src/helper/openUrl')


class Server {
  constructor(config){
    this.conf=Object.assign({},conf,config)
  }

  start(){
    const server = http.createServer((req,res)=>{
      const filePath = path.join(this.conf.root,req.url)
      route(req,res,filePath,this.conf)
    })
    
    server.listen(this.conf.port,this.conf.hostname,()=>{
      const addr = `http://${this.conf.hostname}:${this.conf.port}`
      console.log(`server started at ${chalk.green(addr)}`);
      openUrl(addr)
    })
  }
}

module.exports = Server

